#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys
import django  # type: ignore
import logging
from datetime import datetime

# 添加项目根目录到Python路径
current_dir = os.path.dirname(os.path.abspath(__file__))
backend_dir = current_dir
sys.path.insert(0, backend_dir)

# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wealth_backend.settings_mysql')
django.setup()

from data_service.models import FundInfo, FundInfoDetail

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def fetch_fund_detail_top3():
    """获取fund_info_detail表前3条数据"""
    try:
        logger.info("开始查询fund_info_detail表前3条数据...")
        
        # 查询前3条数据
        fund_details = FundInfoDetail.objects.all()[:3]
        
        if not fund_details:
            logger.info("fund_info_detail表中没有数据")
            return
        
        logger.info(f"找到 {len(fund_details)} 条记录")
        
        # 打印详细信息
        for i, detail in enumerate(fund_details, 1):
            logger.info(f"\n=== 第 {i} 条记录 ===")
            logger.info(f"基金代码: {detail.fund_code}")
            logger.info(f"基金名称: {detail.fund_name}")
            logger.info(f"基金全称: {detail.fund_full_name}")
            logger.info(f"基金类型: {detail.fund_type}")
            logger.info(f"基金公司: {detail.fund_company}")
            logger.info(f"基金经理: {detail.fund_manager}")
            logger.info(f"托管银行: {detail.custodian_bank}")
            logger.info(f"成立日期: {detail.establishment_date}")
            logger.info(f"最新规模: {detail.latest_scale}")
            logger.info(f"评级机构: {detail.rating_agency}")
            logger.info(f"基金评级: {detail.fund_rating}")
            logger.info(f"投资策略: {detail.investment_strategy[:100] if detail.investment_strategy else 'N/A'}...")
            logger.info(f"投资目标: {detail.investment_objective[:100] if detail.investment_objective else 'N/A'}...")
            logger.info(f"业绩比较基准: {detail.performance_benchmark[:100] if detail.performance_benchmark else 'N/A'}...")
            logger.info(f"数据源: {detail.data_source}")
            logger.info(f"创建时间: {detail.created_at}")
            logger.info(f"更新时间: {detail.updated_at}")
            
        return fund_details
        
    except Exception as e:
        logger.error(f"查询fund_info_detail表数据失败: {str(e)}")
        return None

def main():
    """主函数"""
    logger.info("开始获取fund_info_detail表前3条数据")
    
    result = fetch_fund_detail_top3()
    
    if result:
        logger.info("\n=== 查询完成 ===")
        logger.info(f"成功获取 {len(result)} 条记录")
    else:
        logger.error("查询失败或无数据")

if __name__ == '__main__':
    main()